package com.atguigu.gulimall.ware.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //key:
        //status:
        //wareId:
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        String key = params.get("key") != null ? (String) params.get("key") : "";
        if (StringUtils.isNotBlank(key)) {
            //purchase_id sku_id
            queryWrapper.and(keyQueryWrapper -> {
                keyQueryWrapper
                        .eq("purchase_id", key)
                        .or()
                        .eq("sku_id", key);
            });
        }

        String status = params.get("status") != null ? (String) params.get("status") : "";
        if (StringUtils.isNotBlank(status)) {
            //status
            queryWrapper.eq("status", status);
        }

        String wareId = params.get("wareId") != null ? (String) params.get("wareId") : "";
        if (StringUtils.isNotBlank(wareId)) {
            //ware_id
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}